﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// Double类型转换接口
    /// </summary>
    public class DoubleTypeConverter : TypeConverterAbs
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public DoubleTypeConverter()
            : base(PrimitiveDataTypes.DoubleType)
        {

        }

        /// <summary>
        /// 将字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="options">类型转换选项参数</param>
        /// <param name="value">要转换的值</param>
        /// <param name="valueType">值类型</param>
        /// <returns>转换后的值</returns>
        protected override object PrimitiveConvertTo(ITypeConverterOptions options, object value, PrimitiveDataTypes valueType)
        {
            object result;
            switch (valueType)
            {
                case PrimitiveDataTypes.DoubleType:
                    result = value;
                    break;
                case PrimitiveDataTypes.StringType:
                    if (options != null && options.Style is NumberStyles)
                    {
                        if (Double.TryParse((string)value, (NumberStyles)options.Style, options.FormatProvider, out var v))
                        {
                            result = v;
                        }
                        else
                        {
                            throw new InvalidCastException($"类型\"{EnumParserManager.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"不支持转换为类型\"Double\",转换失败");
                        }
                    }
                    else
                    {
                        if (options == null || options.FormatProvider == null)
                        {
                            if (Double.TryParse((string)value, out var v))
                            {
                                result = v;
                            }
                            else
                            {
                                throw new InvalidCastException($"类型\"{EnumParserManager.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"不支持转换为类型\"Double\",转换失败");
                            }
                        }
                        else
                        {
                            if (Double.TryParse((string)value, options.FormatProvider, out var v))
                            {
                                result = v;
                            }
                            else
                            {
                                throw new InvalidCastException($"类型\"{EnumParserManager.PrimitiveTypesEnumParser.GetEnumString(valueType)}\"不支持转换为类型\"Double\",转换失败");
                            }
                        }
                    }
                    break;
                default:
                    result = Convert.ToDouble(value);
                    break;
            }
            return result;
        }
        
        /// <summary>
        /// 转换数据到
        /// </summary>
        /// <param name="options">类型转换选项参数</param>
        /// <param name="value">要转换的值</param>
        /// <param name="valueType">值类型</param>
        /// <param name="result">转换后的值,存放在object中,如果转换失败为目标类型的默认值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(ITypeConverterOptions options, object value, PrimitiveDataTypes valueType, out object result)
        {
            bool parseRet = true;
            switch (valueType)
            {
                case PrimitiveDataTypes.DoubleType:
                    result = value;
                    break;
                case PrimitiveDataTypes.StringType:
                    if (options != null && options.Style is NumberStyles)
                    {
                        if (Double.TryParse((string)value, (NumberStyles)options.Style, options.FormatProvider, out var v))
                        {
                            result = v;
                        }
                        else
                        {
                            result = null;
                            parseRet = false;
                        }
                    }
                    else
                    {
                        if (options == null || options.FormatProvider == null)
                        {
                            if (Double.TryParse((string)value, out var v))
                            {
                                result = v;
                            }
                            else
                            {
                                result = null;
                                parseRet = false;
                            }
                        }
                        else
                        {
                            if (Double.TryParse((string)value, options.FormatProvider, out var v))
                            {
                                result = v;
                            }
                            else
                            {
                                result = null;
                                parseRet = false;
                            }
                        }
                    }
                    break;
                default:
                    result = Convert.ToDouble(value);
                    break;
            }

            return parseRet;
        }


    }
}
